package com.zhangyu.shadow.ec.sign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zhangyu.shadow.core.account.AccountManager;
import com.zhangyu.shadow.ec.bean.ApiModel;
import com.zhangyu.shadow.ec.database.DatabaseManager;
import com.zhangyu.shadow.ec.database.UserProfile;

/**
 * @author zhangyu
 * @date 2017/10/26
 */

public class SignHandler {
    public static void onSignUp(String response, ISignListener listener) {
        ApiModel<UserProfile> apiModel = JSON.parseObject(response, new TypeReference<ApiModel<UserProfile>>() {
        });
        final UserProfile userProfile = apiModel.getData();
        DatabaseManager.getInstance().getUserProfileDao().insertOrReplace(userProfile);

        AccountManager.setSignState(true);
        if (listener != null) {
            listener.onSignUpSuccess();
        }
    }
    public static void onSignIn(String response, ISignListener listener) {
        ApiModel<UserProfile> apiModel = JSON.parseObject(response, new TypeReference<ApiModel<UserProfile>>() {
        });
        final UserProfile userProfile = apiModel.getData();
        DatabaseManager.getInstance().getUserProfileDao().insertOrReplace(userProfile);

        AccountManager.setSignState(true);
        if (listener != null) {
            listener.onSignInSuccess();
        }
    }
}
